import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_app/box_2.dart';
import 'package:flutter_app/button_3_4.dart';
import 'package:flutter_app/text3_3.dart';

import 'BoxDecoration5_3.dart';
import 'ClipRoute_5_7.dart';
import 'ConstrainedBox5_2.dart';
import 'CustomScrollView_6_5.dart';
import 'FocusTestRoute_3_7.dart';
import 'FormTestRoute_3_7_2.dart';
import 'GridView_6_4.dart';
import 'Image_3_5.dart';
import 'Image_3_5_2.dart';
import 'ListVeiw6_3.dart';
import 'Padding5_1.dart';
import 'ProgressRoutePage.dart';
import 'Row_Column_4_2.dart';
import 'ScaffoldPage.dart';
import 'ScrollControllerTestRoute_6_6_1.dart';
import 'ScrollNotificationTestRoute_6_6_2.dart';
import 'Stack4_5.dart';
import 'Switch_3_6.dart';
import 'TextField_3_7.dart';
import 'Transform5_4.dart';
import 'box_1.dart';

void collectLog(String line) {
  //收集日志
}

void reportErrorAndLog(FlutterErrorDetails details) {
  //上报错误和日志逻辑
}

FlutterErrorDetails? makeDetails(Object? obj, StackTrace? stack) {
  // 构建错误信息
}

void main() {
  runZoned(
    () => runApp(MyApp()),
    zoneSpecification: ZoneSpecification(
      print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
        collectLog(line); // 收集日志
      },
    ),
    onError: (Object obj, StackTrace stack) {
      var details = makeDetails(obj, stack);
      reportErrorAndLog(details!);
    },
  );
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      routes: {
        "box_1": (context) => BoxParentPage(),
        "box_2": (context) => BoxParentPageC(),
        "text_1": (context) => TextPageWidget(),
        "button_3_4": (context) => ButtonPage(),
        "image_3_5": (context) => Image3_5Page(),
        "image_3_5_2": (context) => Image3_5_2Page(),
        "SwitchPage_3_6": (context) => SwitchPage(),
        "TextField_3_7": (context) => TextFieldPage(),
        "FocusTestRoute": (context) => FocusTestRoute(),
        "FormTestRoutePage": (context) => FormTestRoute(),
        "ProgressRoutePage": (context) => ProgressRoutePage(),
        "Row_Column_4_2": (context) => Row_Column_4_2(),
        "Stack4_5": (context) => Stack4_5(),
        "Padding_4_2": (context) => Padding_4_2(),
        "ConstrainedBox5_2": (context) => ConstrainedBox5_2(),
        "BoxDecoration5_3": (context) => BoxDecoration5_3(),
        "Transform5_4": (context) => Transform5_4(),
        "Container5_5": (context) => Container5_5(),
        "ScaffoldPage": (context) => ScaffoldPage(),
        "ClipTest_5_7": (context) => ClipTest_5_7(),
        "ListView6_3": (context) => ListView6_3(),
        "GridView_6_4": (context) => GridView_6_4(),
        "CustomScrollViewTestRoute": (context) => CustomScrollViewTestRoute(),
        "ScrollControllerTestRoute_6_6_1": (context) =>
            ScrollControllerTestRoute_6_6_1(),
        "ScrollNotificationTestRoute_6_6_2": (context) =>
            ScrollNotificationTestRoute_6_6_2(),
      },
      home: MyHomePage(title: 'Flutter Demo Home Page'),
      // home: CounterWidget(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String? title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("子树中获取State对象"),
      ),
      body: Padding(
          padding: EdgeInsets.all(18),
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                RaisedButton(
                  onPressed: () => {Navigator.of(context).pushNamed("box_1")},
                  child: Text("打开box"),
                ),
                RaisedButton(
                  onPressed: () => {Navigator.of(context).pushNamed("box_2")},
                  child: Text("打开Box2"),
                ),
                RaisedButton(
                  onPressed: () => {Navigator.of(context).pushNamed("text_1")},
                  child: Text("打开Text1"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("button_3_4");
                  },
                  child: Text("button"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("image_3_5");
                  },
                  child: Text("Image"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("image_3_5_2");
                  },
                  child: Text("Image2"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("SwitchPage_3_6");
                  },
                  child: Text("SwitchPage"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("TextField_3_7");
                  },
                  child: Text("TextField"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("FocusTestRoute");
                  },
                  child: Text("FocusTestRoute"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("FormTestRoutePage");
                  },
                  child: Text("FormTestRoutePage"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("ProgressRoutePage");
                  },
                  child: Text("ProgressRoutePage"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("Row_Column_4_2");
                  },
                  child: Text("Row_Column_4_2"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("Stack4_5");
                  },
                  child: Text("Stack4_5"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("Padding_4_2");
                  },
                  child: Text("Padding_4_2"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("ConstrainedBox5_2");
                  },
                  child: Text("ConstrainedBox5_2"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("BoxDecoration5_3");
                  },
                  child: Text("BoxDecoration5_3"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("Transform5_4");
                  },
                  child: Text("Transform5_4"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("Container5_5");
                  },
                  child: Text("Container5_5"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("ScaffoldPage");
                  },
                  child: Text("ScaffoldPage"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("ClipTest_5_7");
                  },
                  child: Text("ClipTest_5_7"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("ListView6_3");
                  },
                  child: Text("ListView6_3"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context).pushNamed("GridView_6_4");
                  },
                  child: Text("GridView_6_4"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context)
                        .pushNamed("CustomScrollViewTestRoute");
                  },
                  child: Text("CustomScrollViewTestRoute"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context)
                        .pushNamed("ScrollControllerTestRoute_6_6_1");
                  },
                  child: Text("ScrollControllerTestRoute_6_6_1"),
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.of(context)
                        .pushNamed("ScrollNotificationTestRoute_6_6_2");
                  },
                  child: Text("ScrollNotificationTestRoute_6_6_2"),
                )
              ],
            ),
          )),
    );
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    // return Scaffold(
    //   appBar: AppBar(
    //     // Here we take the value from the MyHomePage object that was created by
    //     // the App.build method, and use it to set our appbar title.
    //     title: Text(widget.title),
    //   ),
    //   body: Center(
    //     // Center is a layout widget. It takes a single child and positions it
    //     // in the middle of the parent.
    //     child: Column(
    //       // Column is also a layout widget. It takes a list of children and
    //       // arranges them vertically. By default, it sizes itself to fit its
    //       // children horizontally, and tries to be as tall as its parent.
    //       //
    //       // Invoke "debug painting" (press "p" in the console, choose the
    //       // "Toggle Debug Paint" action from the Flutter Inspector in Android
    //       // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
    //       // to see the wireframe for each widget.
    //       //
    //       // Column has various properties to control how it sizes itself and
    //       // how it positions its children. Here we use mainAxisAlignment to
    //       // center the children vertically; the main axis here is the vertical
    //       // axis because Columns are vertical (the cross axis would be
    //       // horizontal).
    //       mainAxisAlignment: MainAxisAlignment.center,
    //       children: <Widget>[
    //         Text(
    //           'You have pushed the button this many times:',
    //         ),
    //         Text(
    //           '$_counter',
    //           style: Theme.of(context).textTheme.headline4,
    //         ),
    //       ],
    //     ),
    //   ),
    //   floatingActionButton: FloatingActionButton(
    //     onPressed: _incrementCounter,
    //     tooltip: 'Increment',
    //     child: Icon(Icons.add),
    //   ), // This trailing comma makes auto-formatting nicer for build methods.
    // );
  }
}

class CounterWidget extends StatefulWidget {
  final int initValue;

  @override
  _CounterWidgetState createState() {
    // TODO: implement createState
    return new _CounterWidgetState();
  }

  const CounterWidget({Key? key, this.initValue: 0});
}

class _CounterWidgetState extends State<CounterWidget> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    _counter = widget.initValue;
    print("initSate");
  }

  @override
  Widget build(BuildContext context) {
    print("build");
    return Scaffold(
      body: Center(
        child: FlatButton(
          child: Text('$_counter'),
          onPressed: () => setState(() => ++_counter),
        ),
      ),
    );
  }

  @override
  void didUpdateWidget(CounterWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    print("didUpdateWidget");
  }

  @override
  void deactivate() {
    super.deactivate();
    print("deactivate");
  }

  @override
  void dispose() {
    super.dispose();
    print("dispose");
  }

  @override
  void reassemble() {
    super.reassemble();
    print("reassemble");
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("didChangeDependencies");
  }
}
